package com.stone.databinding.ui;

import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;

import androidx.databinding.BindingAdapter;
import androidx.databinding.InverseBindingAdapter;
import androidx.databinding.InverseBindingListener;

import com.stone.databinding.R;

import java.util.ArrayList;

public class NumberOfSetsBindingAdapters {
    /**
     * Needs to be used with [NumberOfSetsConverters.setArrayToString].
     */
    @BindingAdapter("numberOfSets")
    public static void setNumberOfSets(EditText view, String value) {
        view.setText(value);
    }

    /**
     * Called when the [InverseBindingListener] of the `numberOfSetsAttrChanged` binding adapter
     * is notified of a change.
     *
     * Used with the inverse method of [NumberOfSetsConverters.setArrayToString], which is
     * [NumberOfSetsConverters.stringToSetArray].
     */
    @InverseBindingAdapter(attribute = "numberOfSets")
    public static String getNumberOfSets(EditText editText) {
        return editText.getText().toString();
    }

    /**
     * That this Binding Adapter is not defined in the XML. "AttrChanged" is a special
     * suffix that lets you manage changes in the value, using two-way Data Binding.
     *
     * Note that setting a [View.OnFocusChangeListener] overrides other listeners that might be set
     * with `android:onFocusChangeListener`. Consider supporting both in the same binding adapter
     * with `requireAll = false`. See [android.databinding.adapters.CompoundButtonBindingAdapter]
     * for an example.
     */
    @BindingAdapter("numberOfSetsAttrChanged")
    public static void setListener(EditText view, InverseBindingListener listener) {
        view.setOnFocusChangeListener(new View.OnFocusChangeListener() {
            @Override
            public void onFocusChange(View focusedView, boolean hasFocus) {
                TextView textView = (TextView) focusedView;
                if (hasFocus) {
                    textView.setText("");
                } else {
                    listener.onChange();
                }
            }
        });
    }

    /* This sample showcases the NumberOfSetsConverters below, but note that they could be used
    also like: */

    @BindingAdapter("numberOfSets_alternative")
    public static void setNumberOfSets_alternative(EditText view, ArrayList<Integer> value) {
        view.setText(view.getResources().getString(R.string.sets_format,
                        value.get(0) + 1,
                        value.get(1)));
    }

    @InverseBindingAdapter(attribute = "numberOfSets_alternative")
    public static ArrayList<Integer> getNumberOfSets_alternative(EditText editText) {
        ArrayList<Integer> result = new ArrayList<>();
        result.add(0);
        if (TextUtils.isEmpty(editText.getText())) {
            result.add(0);
        } else {
            try {
                result.add(Integer.parseInt(editText.getText().toString()));
            } catch (NumberFormatException e) {
                Log.e("NumberOfSetsBindingAdapters", "getNumberOfSets_alternative failed: " + e.getMessage());
                result.add(0);
            }
        }
        return result;
    }
}
